import type { UserConfig, ConfigEnv } from 'vite';
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path';
import { loadEnv } from "./load-env"
import AutoImport from 'unplugin-auto-import/vite'
function pathResolve(dir: string) {
    return resolve(process.cwd(), '.', dir);
}

export default ({ command, mode }: ConfigEnv): UserConfig => {
    const { VITE_PORT, VITE_BASE_URL } = loadEnv(mode) as GlobEnv;
    return {
        plugins: [vue(), AutoImport({
            // 可以自定义文件生成的位置，默认是根目录下，使用ts的建议放src目录下
            dts: 'src/auto-imports.d.ts',
            imports: ['vue', 'vue-router', 'pinia'],
            eslintrc: {
                enabled: true
            }
        })],
        resolve: {
            alias: [
                {
                    find: '@',
                    replacement: pathResolve('src') + '/',
                },
            ],
            dedupe: ['vue'],
        },
        css: {
            preprocessorOptions: {
                scss: {
                    charset: false,
                    additionalData: `@import "src/styles/variable.scss";`,
                },
            },
        },
        server: {
            port: VITE_PORT,
            proxy: {
                "/api": {
                    target: VITE_BASE_URL,
                    changeOrigin: true
                }
            }
        }
    }
}
